import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
    state: {
        //购物车的数据
        cartList: uni.getStorageSync("cart") || []
    },
    getters: {
        //总件数
        doneTodos: state => {
            let res = 0;
            state.cartList.forEach((v) => {
                if (v.checked) {
                    res += v.count;
                }
            });
            return res;
        },
        //总价格
        total: state => {
            let sum = 0;
            state.cartList.forEach((v) => {
                if (v.checked) {
                    sum += v.goods_price * v.count;
                }
            });
            return sum
        },
        //选中要支付的商品
        pitchList: state => {
            let res = []
            state.cartList.forEach((v) => {
                if (v.checked) {
                    res.push(v)
                }
            });
            return res;
        }
    },

    //定义对象
    mutations: {
        cartListNum(state, list) {
            state.cartList = list
            uni.setStorageSync("cart", state.cartList)
        }
    }
});
export default store;
